package com.mz.ozy.activity.c2c;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mz.ozy.R;
import com.mz.ozy.adapter.C2cTradeAdapter;
import com.mz.ozy.base.BaseFragment;
import com.mz.ozy.bean.C2cTradeBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.C2cTradePresenter;
import com.mz.ozy.mvp.view.C2cTradeContract;
import com.mz.ozy.utils.SPUtils;
import com.mz.ozy.utils.StringUtils;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.widget.dialog.PayPasswordDialog;
import com.mz.ozy.widget.dialog.PayPopupWindow;
import com.mz.ozy.widget.dialog.SellCardDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class C2cTradeFragment extends BaseFragment<C2cTradeContract.View, C2cTradePresenter> implements C2cTradeContract.View {

    private String type;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.tv_compare_price)
    TextView tvComparePrice;
    Unbinder unbinder;
    private PayPopupWindow mPayPopupWindow;
    private C2cTradeAdapter adapter;
    private int pageNo = 1;
    private int tag = 0;
    private List<C2cTradeBean.PageBean.PageItemsBean> beanList = new ArrayList<>();
    private String usdtByCNY;

    public static C2cTradeFragment newInstance(String type) {
        C2cTradeFragment fragment = new C2cTradeFragment();
        Bundle args = new Bundle();
        args.putString("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getString("type");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_assets, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    public void initView() {
        llNoData.setVisibility(View.GONE);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new C2cTradeAdapter(beanList, type);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapt, View view, int position) {
                C2cTradeBean.PageBean.PageItemsBean itemsBean = adapter.getItem(position);
                switch (view.getId()) {
                    case R.id.tv_submit: {
                        // if(dataItem.)
                        SellCardDialog sellCardDialog = new SellCardDialog(getActivity(), type, usdtByCNY, itemsBean);
                        sellCardDialog.setOnDialogListener(new SellCardDialog.onDialogListener() {
                            @Override
                            public void onCancel() {
                                sellCardDialog.toggleDialog();
                            }

                            @Override
                            public void onSubmit(final String number, final String paytype) {
                                sellCardDialog.toggleDialog();
                                if ("0".equals(paytype)) {
                                    showSportsHarmDialog(itemsBean.getId(), paytype, number);
                                } else {
                                    mPresenter.submitOrder(getActivity(), String.valueOf(itemsBean.getId()), paytype, number, null);
                                }
                            }
                        });
                        sellCardDialog.toggleDialog();
                    }
                }

            }
        });
        smartRefreshLayout.setEnableAutoLoadmore(true);
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                tag = 1;
                pageNo = pageNo + 1;
                mPresenter.geAssetsInfo(mActivity, type, String.valueOf(pageNo), false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshList(false);
            }
        });

        if ("1".equals(type)) {
            refreshList(true);
        } else {
            refreshList(false);
        }
    }

    private void refreshList(boolean load) {
        if (mPresenter == null) return;
        tag = 0;
        pageNo = 1;
        mPresenter.geAssetsInfo(mActivity, type, String.valueOf(pageNo), load);
    }

    private void showSportsHarmDialog(final int id, String paytype, final String number) {
        PayPasswordDialog mSportsHarmDialog = new PayPasswordDialog(getActivity());
        mSportsHarmDialog.setOnDialogListener(new PayPasswordDialog.onDialogListener() {
            @Override
            public void onSubmit(String pass) {
                mPresenter.submitOrder(getActivity(), String.valueOf(id), paytype, number, pass);
                mSportsHarmDialog.toggleDialog();
            }

            @Override
            public void onCancel() {
                mSportsHarmDialog.toggleDialog();
            }
        });
        mSportsHarmDialog.toggleDialog();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void geAssetsInfoSuccess(C2cTradeBean bean) {
        usdtByCNY = bean.getUsdtByCNY();
        SPUtils.getInstance(getActivity()).putString(Constants.SPName.USDTBYCNY, usdtByCNY);
        tvComparePrice.setText(StringUtils.formattingDecimal(bean.getPrice(), 2) + " CNY");
        if (tag == 0) {
            smartRefreshLayout.setLoadmoreFinished(false);
            smartRefreshLayout.finishRefresh();
            beanList.clear();
            if (bean.getOrder().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            smartRefreshLayout.finishLoadmore();
        }
        beanList.addAll(bean.getOrder().getPageItems());
        adapter.notifyDataSetChanged();
        if (bean.getOrder().getPageItems().size() == 0 || bean.getOrder().getPageSize() * bean.getOrder().getPageNumber() >= bean.getOrder().getPageTotle()) {
            smartRefreshLayout.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }

    }

    @Override
    public void submitOrderSuccess(C2cTradeBean bean) {
        ToastUtils.showToast("购买成功");
        //startActivity(new Intent(getActivity(), OrderListActivity.class));
    }
}
